<template>
	<view>
		<view class="uni-flex uni-row"  >
			<image class="uni-card-media-logo" src="../../static/image/found/signature.png"></image>
		<view style="flex: 1;">
			<!-- <view class="uni-card">
						
					</view> -->
							<view class="uni-card-media-body ">
								
									
								<text class="uni-card-media-text-top" >{{user.nickname}}</text>
								<text class="uni-card-media-text-bottom">
										<text class="text" v-if="isContinueSign">您已经连续签到:{{user.contSignDays}}</text>
										<text class="text" v-else>您还没有签到，赶快签到吧</text>
								</text>
			
								
								
							</view>
		</view>
		<view style="width: 120upx; text-align: center; align-items: center; vertical-align: middle;" >
			<text class="uni-border-success uni-margin-10"    @tap="sign()" v-if="!isContinueSign">+ 签到</text>
			<text class="uni-border-gray uni-margin-10"   v-if="isContinueSign">已签到</text>
		</view>
			
		<!-- <view class="uni-card" style="width: 160upx; ">
			<view class="uni-card-header uni-card-media" style="height: 100px;">
				
			</view>
		</view> -->
		
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations,mapGetters } from 'vuex'
	
	import {addSign} from '@/api/ucenter/index'

	export default {
		data() {
			return {
				isContinueSign: false,
			}
		},
		computed: {
			...mapState({
						user: state => state.user.userInfo,
			}),
		},
		mounted() {
			let  lastSignStamp = new Date(this.user.lastSignStamp);
			if(lastSignStamp.getYear() == new Date().getYear()
			&& lastSignStamp.getMonth() == new Date().getMonth()
			&& lastSignStamp.getDay() == new Date().getDay())
			{
				this.isContinueSign = true
			}
		},
		methods: {
			 ...mapMutations(
				["SET_USER_INFO"]
			),
			sign() {
				var partySign = {
					singType: 'signType',
					createdBy: this.user.partyId,
					createdName: this.user.nickname,
					lastUpdatedBy: this.user.partyId,
					lastUpdatedName: this.user.nickname,
				}
				let contSignDays = 1
				if(this.user.contSignDays !== undefined && this.user.contSignDays !== null) {
					contSignDays = this.user.contSignDays
				}
				if(this.user.lastSignStamp!== undefined && this.user.lastSignStamp !== null)
				{
					let  lastSignStamp =this.user.lastSignStamp;
					
					if(lastSignStamp.getYear() == new Date().getYear()
					&& lastSignStamp.getMonth() == new Date().getMonth()
					&& lastSignStamp.getDay()+1 == new Date().getDay()) {
						contSignDays = contSignDays+1
					}
					
					if(lastSignStamp.getDay() == 1) {
						contSignDays = 1;
					}
				}
				
				addSign(partySign).then(response => {
					if(response.status == 200 && response.rel == true) {
						this.user.lastSignStamp = new Date();
						this.user.contSignDays = contSignDays
						this.user.totalValue = this.user.totalValue + 5;
						this.SET_USER_INFO(this.user);
						uni.showModal({
							title:"提示",
							content:"签到积分+5"
						})
					} else {
						uni.showModal({
							title:"错误",
							content:"签到记录错误，请重试，谢谢"
						})
					}
				}).catch(err => {
					uni.showModal({
						title:"错误",
						content:"签到记录错误，请重试，谢谢"
					})
				})
			}
		}
	}
</script>

<style>
	.uni-card {
		margin-bottom: 0upx;
		box-shadow: none;
		border: none;
	}
</style>
